library(ggplot2)
library(qqplotr)
## 
## Attaching package: 'qqplotr'
## The following objects are masked from 'package:ggplot2':
## 
##     stat_qq_line, StatQqLine
library(tidyr)
library(stats4)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(onewaytests)
## 
## Attaching package: 'onewaytests'
## The following object is masked from 'package:psych':
## 
##     describe
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(nonpar)
caminhao <- read.csv2("~/caminhao.csv")
str(caminhao)
## 'data.frame':    36 obs. of  2 variables:
##  $ Tempo: num  2.3 2.7 2.9 3 3.1 2.7 3.2 3.4 3.1 2 ...
##  $ Tipo : chr  "bau" "bau" "bau" "bau" ...
summary(caminhao)
##      Tempo           Tipo          
##  Min.   :1.300   Length:36         
##  1st Qu.:2.700   Class :character  
##  Median :2.950   Mode  :character  
##  Mean   :2.983                     
##  3rd Qu.:3.300                     
##  Max.   :4.500
Tipo_caminhao <- split(caminhao, caminhao$Tipo)
#Boxplot comparando os 2 tipos de camnhões 
boxplot(caminhao$Tempo ~ caminhao$Tipo)

#Teste de Normalidade para bau
#H0: A distribuição é Normal
#Ha: A distribuição não é Normal

ggplot(mapping = aes(sample = Tipo_caminhao$bau$Tempo)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")

shapiro.test(Tipo_caminhao$bau$Tempo)
## 
##  Shapiro-Wilk normality test
## 
## data:  Tipo_caminhao$bau$Tempo
## W = 0.93249, p-value = 0.1544
#Teste de Normalidade para bau
ggplot(mapping = aes(sample = Tipo_caminhao$lonado$Tempo)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")

shapiro.test(Tipo_caminhao$lonado$Tempo)
## 
##  Shapiro-Wilk normality test
## 
## data:  Tipo_caminhao$lonado$Tempo
## W = 0.94466, p-value = 0.4446
#Teste para variancias, assumindo 2 distribuições Normais
#H0: Var(bau) = Var(lonado)
#Ha: Var(bau) <> Var(lonado)
var.test(caminhao$Tempo ~ caminhao$Tipo)
## 
##  F test to compare two variances
## 
## data:  caminhao$Tempo by caminhao$Tipo
## F = 0.14859, num df = 20, denom df = 14, p-value = 0.0001541
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.05225211 0.38677672
## sample estimates:
## ratio of variances 
##          0.1485889
#Teste para médias, assumindo variancias diferentes
#H0: Media(bau) = Media(lonado)
#Ha: Media(bau) <> Media(lonado)

t.test(caminhao$Tempo ~ caminhao$Tipo, var.equal = FALSE)
## 
##  Welch Two Sample t-test
## 
## data:  caminhao$Tempo by caminhao$Tipo
## t = -1.4855, df = 16.995, p-value = 0.1557
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.8712957  0.1512957
## sample estimates:
##    mean in group bau mean in group lonado 
##             2.833333             3.193333
vinhos <- read.csv2("~/Base_Vinhos_2018.csv")
str(vinhos)
## 'data.frame':    100 obs. of  15 variables:
##  $ id_vinho          : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ fixedacidity      : num  6.6 6.7 10.6 5.4 6.7 6.8 6.6 7.2 5.1 6.2 ...
##  $ volatileacidity   : num  0.24 0.34 0.31 0.18 0.3 0.5 0.61 0.66 0.26 0.22 ...
##  $ citricacid        : num  0.35 0.43 0.49 0.24 0.44 0.11 0 0.33 0.33 0.2 ...
##  $ residualsugar     : num  7.7 1.6 2.2 4.8 18.8 ...
##  $ chlorides         : num  0.031 0.041 0.063 0.041 0.057 0.075 0.069 0.068 0.027 0.035 ...
##  $ freesulfurdioxide : int  36 29 18 30 65 16 4 34 46 58 ...
##  $ totalsulfurdioxide: num  135 114 40 113 224 49 8 102 113 184 ...
##  $ density           : num  0.994 0.99 0.998 0.994 1 ...
##  $ pH                : num  3.19 3.23 3.14 3.42 3.11 3.36 3.33 3.27 3.35 3.11 ...
##  $ sulphates         : num  0.37 0.44 0.51 0.4 0.53 0.79 0.37 0.78 0.43 0.53 ...
##  $ alcohol           : num  10.5 12.6 9.8 9.4 9.1 9.5 10.4 12.8 11.4 9 ...
##  $ quality           : int  5 6 6 6 5 5 4 6 7 6 ...
##  $ Vinho             : chr  "WHITE" "WHITE" "RED" "WHITE" ...
##  $ avaliacao         : chr  "RUIM" "BOM" "BOM" "BOM" ...
summary(vinhos)
##     id_vinho       fixedacidity    volatileacidity    citricacid    
##  Min.   :  1.00   Min.   : 4.600   Min.   :0.1200   Min.   :0.0000  
##  1st Qu.: 25.75   1st Qu.: 6.600   1st Qu.:0.2500   1st Qu.:0.2075  
##  Median : 50.50   Median : 7.050   Median :0.3550   Median :0.3050  
##  Mean   : 50.50   Mean   : 7.462   Mean   :0.4061   Mean   :0.2931  
##  3rd Qu.: 75.25   3rd Qu.: 8.000   3rd Qu.:0.5625   3rd Qu.:0.3825  
##  Max.   :100.00   Max.   :14.300   Max.   :0.7850   Max.   :1.0000  
##  residualsugar      chlorides       freesulfurdioxide totalsulfurdioxide
##  Min.   : 1.100   Min.   :0.01400   Min.   : 3.00     Min.   :  8.00    
##  1st Qu.: 1.875   1st Qu.:0.04200   1st Qu.:13.75     1st Qu.: 37.75    
##  Median : 2.300   Median :0.05650   Median :20.00     Median : 91.50    
##  Mean   : 4.403   Mean   :0.06448   Mean   :24.91     Mean   : 90.61    
##  3rd Qu.: 5.525   3rd Qu.:0.07575   3rd Qu.:34.00     3rd Qu.:134.00    
##  Max.   :20.800   Max.   :0.41400   Max.   :65.00     Max.   :224.00    
##     density             pH          sulphates         alcohol     
##  Min.   :0.9895   Min.   :2.860   Min.   :0.2800   Min.   : 8.40  
##  1st Qu.:0.9934   1st Qu.:3.140   1st Qu.:0.4575   1st Qu.: 9.50  
##  Median :0.9956   Median :3.260   Median :0.5500   Median :10.30  
##  Mean   :0.9952   Mean   :3.267   Mean   :0.5720   Mean   :10.47  
##  3rd Qu.:0.9974   3rd Qu.:3.370   3rd Qu.:0.6625   3rd Qu.:11.30  
##  Max.   :1.0022   Max.   :4.010   Max.   :1.3300   Max.   :13.20  
##     quality        Vinho            avaliacao        
##  Min.   :4.00   Length:100         Length:100        
##  1st Qu.:5.00   Class :character   Class :character  
##  Median :6.00   Mode  :character   Mode  :character  
##  Mean   :5.75                                        
##  3rd Qu.:6.00                                        
##  Max.   :8.00
boxplot(vinhos$pH ~ vinhos$Vinho)

Cor_vinhos <- split(vinhos, vinhos$Vinho)
#Teste de Normalidade para pH Brancos
#H0: A distribuição é Normal
#Ha: A distribuição não é Normal

ggplot(mapping = aes(sample = Cor_vinhos$WHITE$pH)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")

shapiro.test(Cor_vinhos$WHITE$pH)
## 
##  Shapiro-Wilk normality test
## 
## data:  Cor_vinhos$WHITE$pH
## W = 0.94685, p-value = 0.02537
#Teste de Normalidade para pH Tintos
#H0: A distribuição é Normal
#Ha: A distribuição não é Normal

ggplot(mapping = aes(sample = Cor_vinhos$RED$pH)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")

shapiro.test(Cor_vinhos$RED$pH)
## 
##  Shapiro-Wilk normality test
## 
## data:  Cor_vinhos$RED$pH
## W = 0.87985, p-value = 0.0001108
#Teste de hipótese para Variância - Teste de Levene - Distribuições não Normais
#H0: Var(pH Brancos) = Var(pH Tintos)
#Ha: Var(pH Brancos) <> Var(pH Tintos)

var(Cor_vinhos$RED$pH)
## [1] 0.03639302
var(Cor_vinhos$WHITE$pH)
## [1] 0.0208949
leveneTest(vinhos$pH, vinhos$Vinho)
## Warning in leveneTest.default(vinhos$pH, vinhos$Vinho): vinhos$Vinho coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  1  0.2654 0.6076
##       98
#Intervalo de Confiança para Variância

ICVariancia <- function(a, b){
n<-length(a)
s2<-var(a)
ic.sigma2<-(n-1)* s2/qchisq(c(1-((1-b)/2), (1-b)/2), n-1)
ic.sigma2
}
ICVariancia(Cor_vinhos$RED$pH, 0.95)
## [1] 0.02539443 0.05651284
ICVariancia(Cor_vinhos$WHITE$pH, 0.95)
## [1] 0.01458010 0.03244661
#Teste de hipótese para Variância - Teste de Cochrans - Distribuições não Normais
#H0: Var(pH Brancos) = Var(pH Tintos)
#Ha: Var(pH Brancos) <> Var(pH Tintos)

EmColunas <- cbind(Cor_vinhos$RED$pH, Cor_vinhos$WHITE$pH)

cochrans.q(EmColunas)
## 
##  Cochran's Q Test 
##  
##  H0: There is no difference in the effectiveness of treatments. 
##  HA: There is a difference in the effectiveness of treatments. 
##  
##  Q = -0.0208329430278958 
##  
##  Degrees of Freedom = 1 
##  
##  Significance Level = 0.05 
##  The p-value is  1 
##   
## 
#Teste de hipótese para Média, assumindo variancias iguais
#H0: Media(pH Brancos) = Media(pH Tintos)
#Ha: Media(pH Brancos) <> Media(pH Tintos)

t.test(vinhos$pH ~ vinhos$Vinho, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  vinhos$pH by vinhos$Vinho
## t = 3.2852, df = 98, p-value = 0.001415
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  0.04402766 0.17837234
## sample estimates:
##   mean in group RED mean in group WHITE 
##              3.3222              3.2110
boxplot(vinhos$alcohol ~ vinhos$Vinho)

#Teste de Normalidade para alcool Brancos
#H0: A distribuição é Normal
#Ha: A distribuição não é Normal

ggplot(mapping = aes(sample = Cor_vinhos$WHITE$alcohol)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")

shapiro.test(Cor_vinhos$WHITE$alcohol)
## 
##  Shapiro-Wilk normality test
## 
## data:  Cor_vinhos$WHITE$alcohol
## W = 0.96534, p-value = 0.1487
#Teste de Normalidade para alcool Tintos
#H0: A distribuição é Normal
#Ha: A distribuição não é Normal

ggplot(mapping = aes(sample = Cor_vinhos$RED$alcohol)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")

shapiro.test(Cor_vinhos$RED$alcohol)
## 
##  Shapiro-Wilk normality test
## 
## data:  Cor_vinhos$RED$alcohol
## W = 0.89663, p-value = 0.0003725
#Teste de hipótese para Variância - Teste de Levene - Distribuições não Normais
#H0: Var(pH Brancos) = Var(pH Tintos)
#Ha: Var(pH Brancos) <> Var(pH Tintos)

var(Cor_vinhos$RED$alcohol)
## [1] 1.400771
var(Cor_vinhos$WHITE$alcohol)
## [1] 1.284858
leveneTest(vinhos$alcohol, vinhos$Vinho)
## Warning in leveneTest.default(vinhos$alcohol, vinhos$Vinho): vinhos$Vinho
## coerced to factor.
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  1  0.1461 0.7031
##       98
#Intervalo de Confiança para Variância

ICVariancia <- function(a, b){
n<-length(a)
s2<-var(a)
ic.sigma2<-(n-1)* s2/qchisq(c(1-((1-b)/2), (1-b)/2), n-1)
ic.sigma2
}
ICVariancia(Cor_vinhos$RED$alcohol, 0.95)
## [1] 0.9774344 2.1751856
ICVariancia(Cor_vinhos$WHITE$alcohol, 0.95)
## [1] 0.8965521 1.9951899
#Teste de hipótese para Variância - Teste de Cochrans - Distribuições não Normais
#H0: Var(pH Brancos) = Var(pH Tintos)
#Ha: Var(pH Brancos) <> Var(pH Tintos)

EmColunas <- cbind(Cor_vinhos$RED$alcohol, Cor_vinhos$WHITE$alcohol)

cochrans.q(EmColunas)
## 
##  Cochran's Q Test 
##  
##  H0: There is no difference in the effectiveness of treatments. 
##  HA: There is a difference in the effectiveness of treatments. 
##  
##  Q = -0.00612063085318956 
##  
##  Degrees of Freedom = 1 
##  
##  Significance Level = 0.05 
##  The p-value is  1 
##   
## 
#Teste de hipótese para Média
#H0: Media(alcool Brancos) = Media(alcool Tintos)
#Ha: Media(alcool Brancos) <> Media(alcool Tintos)

t.test(vinhos$alcohol ~ vinhos$Vinho, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  vinhos$alcohol by vinhos$Vinho
## t = -0.95357, df = 98, p-value = 0.3426
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.6809197  0.2389197
## sample estimates:
##   mean in group RED mean in group WHITE 
##              10.362              10.583
Ipem <- read.csv2("~/ipem.csv")
boxplot(Ipem$Massa ~ Ipem$Marca)

Marca_Ipem <- split(Ipem, Ipem$Marca)
#Teste de Normalidade
#H0: A distribuição é Normal
#Ha: A distribuição não é Normal
Papel_Normal = function(a)
{
ggplot(mapping = aes(sample = a)) + stat_qq_point(size = 2, color = "red") + stat_qq_line(color = "green") + xlab ("x-axis") + ylab("y-axis")
}
  
Teste_Normal = function(a)
{
shapiro.test(a)
}
Papel_Normal (Marca_Ipem$A$Massa)

Teste_Normal(Marca_Ipem$A$Massa)
## 
##  Shapiro-Wilk normality test
## 
## data:  a
## W = 0.91294, p-value = 0.456
Papel_Normal (Marca_Ipem$B$Massa)

Teste_Normal(Marca_Ipem$B$Massa)
## 
##  Shapiro-Wilk normality test
## 
## data:  a
## W = 0.91629, p-value = 0.4791
Papel_Normal (Marca_Ipem$C$Massa)

Teste_Normal(Marca_Ipem$C$Massa)
## 
##  Shapiro-Wilk normality test
## 
## data:  a
## W = 0.95553, p-value = 0.7847
Papel_Normal (Marca_Ipem$D$Massa)

Teste_Normal(Marca_Ipem$D$Massa)
## 
##  Shapiro-Wilk normality test
## 
## data:  a
## W = 0.91597, p-value = 0.4768
Papel_Normal (Marca_Ipem$E$Massa)

Teste_Normal(Marca_Ipem$E$Massa)
## 
##  Shapiro-Wilk normality test
## 
## data:  a
## W = 0.84612, p-value = 0.1464
#Teste de hipótese para Variância - Teste de Levene
#H0: Var todas iguais
#Ha: Var pelo menos uma diferente

leveneTest(Ipem$Massa, Ipem$Marca)
## Warning in leveneTest.default(Ipem$Massa, Ipem$Marca): Ipem$Marca coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  4  0.8804 0.4898
##       25
#Teste de hipótese para Variância - Teste de Cochrans
#H0: Var todas iguais
#Ha: Var pelo menos uma diferente

EmColunas <- cbind(Marca_Ipem$A$Massa, Marca_Ipem$B$Massa, Marca_Ipem$C$Massa, Marca_Ipem$D$Massa, Marca_Ipem$E$Massa)

cochrans.q(EmColunas)
## 
##  Cochran's Q Test 
##  
##  H0: There is no difference in the effectiveness of treatments. 
##  HA: There is a difference in the effectiveness of treatments. 
##  
##  Q = -0.131213815681054 
##  
##  Degrees of Freedom = 4 
##  
##  Significance Level = 0.05 
##  The p-value is  1 
##   
## 
#Teste de Igualde de medias
#H0: medias todas iguais
#Ha: pelo menos uma media é diferentes das outras 

#Variancias iguas
Anova <- lm (Ipem$Massa ~ Ipem$Marca)
summary(Anova)
## 
## Call:
## lm(formula = Ipem$Massa ~ Ipem$Marca)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.0833 -0.5333  0.1250  0.4000  1.3167 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 13.68333    0.39020  35.068  < 2e-16 ***
## Ipem$MarcaB  2.26667    0.55183   4.108 0.000376 ***
## Ipem$MarcaC -0.01667    0.55183  -0.030 0.976145    
## Ipem$MarcaD  1.05000    0.55183   1.903 0.068648 .  
## Ipem$MarcaE -0.60000    0.55183  -1.087 0.287276    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9558 on 25 degrees of freedom
## Multiple R-squared:  0.5747, Adjusted R-squared:  0.5066 
## F-statistic: 8.444 on 4 and 25 DF,  p-value: 0.0001871
#Variancias diferentes
#welch.test(Massa ~ Marca, data = Ipem)
#Análise de regressão simples
#Nota do vinho (quality) com relação ao % de alcool
plot(vinhos$quality ~  vinhos$alcohol) 

Regressao <- lm(vinhos$quality ~ vinhos$alcohol)
summary(Regressao)
## 
## Call:
## lm(formula = vinhos$quality ~ vinhos$alcohol)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1746 -0.5767  0.1163  0.3829  2.5041 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     4.05770    0.71103   5.707 1.23e-07 ***
## vinhos$alcohol  0.16159    0.06749   2.394   0.0185 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7778 on 98 degrees of freedom
## Multiple R-squared:  0.05527,    Adjusted R-squared:  0.04563 
## F-statistic: 5.733 on 1 and 98 DF,  p-value: 0.01855
plot(Regressao)

#Análise de regressão simples
#Nota do vinho (quality) com relação ao pH
plot(vinhos$quality ~  vinhos$pH) 

Regressao <- lm(vinhos$quality ~ vinhos$pH)
summary(Regressao)
## 
## Call:
## lm(formula = vinhos$quality ~ vinhos$pH)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7069 -0.6771  0.1292  0.3552  2.2236 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   8.9943     1.4461   6.220 1.23e-08 ***
## vinhos$pH    -0.9932     0.4421  -2.247   0.0269 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7803 on 98 degrees of freedom
## Multiple R-squared:  0.04899,    Adjusted R-squared:  0.03928 
## F-statistic: 5.048 on 1 and 98 DF,  p-value: 0.0269
plot(Regressao)

#Análise de regressão simples
#Nota do vinho (quality) com relação ao residuo de acucar
plot(vinhos$quality ~  vinhos$residualsugar) 

Regressao <- lm(vinhos$quality ~ vinhos$residualsugar)
summary(Regressao)
## 
## Call:
## lm(formula = vinhos$quality ~ vinhos$residualsugar)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7186 -0.7162  0.2514  0.2858  2.2855 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           5.68999    0.10971  51.865   <2e-16 ***
## vinhos$residualsugar  0.01363    0.01711   0.797    0.428    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7976 on 98 degrees of freedom
## Multiple R-squared:  0.006434,   Adjusted R-squared:  -0.003704 
## F-statistic: 0.6346 on 1 and 98 DF,  p-value: 0.4276
plot(Regressao)

#Análise de regressão multipla
#Nota do vinho (quality) com relação ao alcool + pH + residuo de acucar
Regressao <- lm(vinhos$quality ~ vinhos$alcohol + vinhos$pH + vinhos$residualsugar)
summary(Regressao)
## 
## Call:
## lm(formula = vinhos$quality ~ vinhos$alcohol + vinhos$pH + vinhos$residualsugar)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.60875 -0.47708  0.01458  0.46236  2.27406 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           7.32326    1.51631   4.830 5.17e-06 ***
## vinhos$alcohol        0.22052    0.06818   3.234  0.00167 ** 
## vinhos$pH            -1.21188    0.44156  -2.745  0.00723 ** 
## vinhos$residualsugar  0.01728    0.01685   1.025  0.30774    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7483 on 96 degrees of freedom
## Multiple R-squared:  0.1434, Adjusted R-squared:  0.1166 
## F-statistic: 5.355 on 3 and 96 DF,  p-value: 0.001877
plot(Regressao)